package com.usian.behavior.filter;

import com.usian.common.threadlocal.UserThreadLocalUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author Fay
 * @date 2022/7/18&19:26
 */
@Order(1)
@WebFilter(filterName = "appTokenFilter", urlPatterns = "/*")
public class AppTokenFilter extends GenericFilterBean {
    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest)req;
        //获取请求头中的id
        String userId = request.getHeader("userId");
        //如果userId==null 或者==0就是未登录状态
        if (userId!=null && !userId.equals("0")){
            UserThreadLocalUtils.setUserId(Integer.valueOf(userId));
        }
        chain.doFilter(req, res);
    }
}
